// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "character/"
import "component/"

Rectangle {
    id: _root
    width: 640
    height: 480
    color: "#000000"

    property bool isMobile: false

    Component.onCompleted: {
        state = "Title";
    }

    Rectangle{
        anchors.horizontalCenter: parent.horizontalCenter
        width: w

        property real w: 480
        property real h: 480
        Title{
            id: _title
            width: parent.w
            height: parent.h
            visible: false
            onPlayStart: {
                _root.state = "Prologue";
                _prologue.start();
            }
            onExit: {
                Qt.quit();
            }
        }
        Prologue{
            id: _prologue
            width: parent.w
            height: parent.h
            visible: false
            onFinish: {
                _root.state = "Play";
                _play.start();
            }
        }
        EyeCatch{
            id: _eyecatch
            width: parent.w
            height: parent.h
            visible: false
            onFinish: {
                _root.state = "Play"
                _play.restart();
            }
        }
        PlayField{
            id: _play
            width: parent.w
            height: parent.h
            visible: false
            onPausePlay: {
                _root.state = "Pause";
            }
            onFinishPlay: {
                switch(id){
                case 0:
                    // やられた
                    _gameover.score = score;
                    _gameover.loopCount = loopcount;
                    _gameover.qbCount = qbcount;
                    _root.state = "GameOver";
                    break;
                case 1:
                    // ユーザー操作
                    break;
                default:
                    break;
                }
                console.debug("finish play");
            }
            onEyeCatch: {
                _root.state = "EyeCatch";
                _eyecatch.start(id);
            }
            onSendInfo: {
                _debug.text = obj.count;
            }
        }
        GameOver{
            id: _gameover
            width: parent.w
            height: parent.h
            visible: false
            score: 0
            loopCount: 0
            qbCount: 0
            onExit: {
                _root.state = "Title"
            }
        }
        Pause{
            id: _pause
            width: parent.w
            height: parent.h
            visible: false
            onRestartPlay: {
                _root.state = "Play"
                _play.restart();
            }
            onBackToTitle: {
                _root.state = "Title";
            }
        }
    }

    //デバッグ
    Text{
        id: _debug
        anchors.bottom: parent.bottom
        color: "#dddddd"
        opacity: 0
    }

    // モバイル用ボタン
    Column{
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        MobileButton{
            source: "./images/btn_left_i.png"
            pressedSource: "../images/btn_left_d.png"
            opacity: 0.5
            visible: isMobile
            onPressed: {
                getCurrentView().pressed(Qt.Key_Left);
            }
            onReleased: {
                getCurrentView().released(Qt.Key_Left);
            }
        }
        MobileButton{
            source: "./images/btn_right_i.png"
            pressedSource: "../images/btn_right_d.png"
            opacity: 0.5
            visible: isMobile
            onPressed: {
                getCurrentView().pressed(Qt.Key_Right);
            }
            onReleased: {
                getCurrentView().released(Qt.Key_Right);
            }
        }
    }
    MobileButton{
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        source: "./images/btn_shot_i.png"
        pressedSource: "../images/btn_shot_d.png"
        opacity: 0.5
        visible: isMobile
        onPressed: {
            getCurrentView().pressed(Qt.Key_Space);
        }
        onReleased: {
            getCurrentView().released(Qt.Key_Space);
        }
    }

    function getCurrentView(){
        var obj = null;
        switch(state){
        case "Title":
            obj = _title;
            break;
        case "Prologue":
            obj = _prologue;
            break;
        case "EyeCatch":
            obj = _eyecatch;
            break;
        case "Play":
            obj = _play;
            break;
        case "GameOver":
            obj = _gameover;
            break;
        case "Pause":
            obj = _pause
            break;
        default:
            break;
        }
        return obj;
    }

    states: [
        State {
            name: "Title"
            PropertyChanges {
                target: _title
                visible: true
                focus: true
            }
        },
        State{
            name: "Prologue"
            PropertyChanges {
                target: _prologue
                visible: true
                focus: true
            }
        },
        State{
            name: "EyeCatch"
            PropertyChanges {
                target: _eyecatch
                visible: true
                focus: true
            }
        },
        State{
            name: "Play"
            PropertyChanges {
                target: _play
                visible: true
                focus: true
            }
        },
        State{
            name: "GameOver"
            PropertyChanges {
                target: _gameover
                visible: true
                focus: true
            }
        },
        State{
            name: "Pause"
            PropertyChanges {
                target: _play
                visible: true
                focus: false
            }
            PropertyChanges {
                target: _pause
                visible: true
                focus: true
            }
        }
    ]
}
